package com.example.springboot.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Slf4j
public class WebConfig implements  WebMvcConfigurer {

    @Autowired
    JwtInterceptor jwtInterceptor;

//    @Override
//    public void configurePathMatch(PathMatchConfigurer configurer) {
//        // 指定controller统一的接口前缀
//        configurer.addPathPrefix("/api", clazz -> clazz.isAnnotationPresent(RestController.class));
//    }
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(jwtInterceptor).addPathPatterns("/api/**").excludePathPatterns("/api/admin/login");
//    }

    // 加自定义拦截器JwtInterceptor，设置拦截规则
     @Override
     public void addInterceptors(InterceptorRegistry registry) {
         registry.addInterceptor(jwtInterceptor)
                 .addPathPatterns("/**")
                 .excludePathPatterns(
                         // Knife4j 文档页面及资源
                         "/doc.html",
                         "/doc.html/**", // 覆盖子路径
                         "/webjars/**",
                         "/swagger-resources/**",
                         "/swagger-resources", // 根路径也需排除
                         "/v2/api-docs",
                         "/v2/api-docs/**",
                         "/swagger-ui.html",
                         "/swagger-ui.html/**",
                         // 其他静态资源
                         "/favicon.ico",
                        // 登录注册接口
//                        "/api/admin/login",
//                        "/api/admin/register"
                         "/admin/login",
                         "admin/register"
                );
    }

//
    //静态资源映射
     @Override
     public void addResourceHandlers(ResourceHandlerRegistry registry) {
         log.info("开始设置静态资源映射 ");
         // Knife4j页面入口
         registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
         // WebJars资源（前端依赖）
         registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
         // Swagger 2核心资源（解决/swagger-resources 404）
         registry.addResourceHandler("/swagger-resources/**")
                 .addResourceLocations("classpath:/META-INF/resources/swagger-resources/");
         registry.addResourceHandler("/v2/api-docs")
                 .addResourceLocations("classpath:/META-INF/resources/v2/api-docs");
         // Swagger UI备用路径
         registry.addResourceHandler("/swagger-ui.html")
                 .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
     }
}

